import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { WorkOrderModel } from '#/api/production/models';

import { isUndefined } from '@vben/utils';

import { getLineListApi } from '#/api/master/line';
import { getFinishedMaterialListApi } from '#/api/master/material';
import {
  useWorkOrderStatusOptions,
  useWorkOrderTypeOptions,
} from '#/api/production/models';

/**
 * 获取表格查询表单的字段配置。
 */
export function useSearchSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
      },
      fieldName: 'wo',
      label: '工单编号',
    },
  ];
}

/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      dependencies: {
        triggerFields: ['wo'],
        disabled(values) {
          return !isUndefined(values.id);
        },
      },
      fieldName: 'wo',
      label: '工单编号',
      rules: 'required',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        showSearch: true,
        optionFilterProp: 'label',
        labelField: 'name',
        valueField: 'id',
        api: getFinishedMaterialListApi,
      },
      fieldName: 'productId',
      label: '产品',
      rules: 'required',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        showSearch: true,
        optionFilterProp: 'label',
        labelField: 'lineName',
        valueField: 'id',
        api: getLineListApi,
      },
      fieldName: 'lineId',
      label: '产线',
      rules: 'required',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: useWorkOrderTypeOptions(),
      },
      fieldName: 'woType',
      label: '工单类型',
      rules: 'required',
      defaultValue: 'Trial',
    },
    {
      component: 'InputNumber',
      componentProps: {
        min: 1,
        precision: 0,
        step: 10,
      },
      fieldName: 'planQty',
      label: '计划生产数量',
      rules: 'required',
      defaultValue: 1000,
    },
    {
      component: 'DatePicker',
      componentProps: {
        valueFormat: 'YYYY-MM-DD',
      },
      fieldName: 'planStartDate',
      label: '计划开始日期',
      rules: 'required',
    },
    {
      component: 'DatePicker',
      componentProps: {
        valueFormat: 'YYYY-MM-DD',
      },
      fieldName: 'planEndDate',
      label: '计划完成日期',
      rules: 'required',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
      },
      fieldName: 'isMaster',
      label: '主工单',
      rules: 'required',
      defaultValue: true,
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'Input',
      fieldName: 'source',
      label: '来源',
    },
    {
      component: 'Input',
      fieldName: 'batchNo',
      label: '批次号',
    },
    {
      component: 'Textarea',
      componentProps: {
        showCount: true,
        maxlength: 128,
      },
      fieldName: 'remark',
      label: '备注',
      formItemClass: 'col-span-2 items-baseline',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<WorkOrderModel>,
): VxeTableGridOptions<WorkOrderModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'wo', title: '工单编号', width: 160 },
    { field: 'product.name', title: '产品', width: 140 },
    { field: 'line.lineName', title: '产线', width: 140 },
    {
      field: 'woType',
      title: '工单类型',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: useWorkOrderTypeOptions(),
      },
    },
    { field: 'planQty', title: '计划生产数量', width: 120 },
    {
      field: 'planStartDate',
      title: '计划开始日期',
      width: 120,
      formatter: 'formatDate',
    },
    {
      field: 'planEndDate',
      title: '计划完成日期',
      width: 120,
      formatter: 'formatDate',
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: useWorkOrderStatusOptions(),
      },
    },
    {
      field: 'isMaster',
      title: '主工单',
      width: 100,
      formatter: ({ cellValue }) => (cellValue ? '是' : '否'),
    },
    { field: 'source', title: '来源', width: 100 },
    { field: 'batchNo', title: '批次号', width: 120 },
    { field: 'remark', title: '备注', minWidth: 100 },
    {
      cellRender: {
        attrs: {
          nameField: 'wo',
          nameTitle: '工单',
          onClick: onActionClick,
        },
        props: {
          disabled: (params: any) => params.status !== 'Draft',
        },
        name: 'CellOperation',
        options: [
          { code: 'issue', text: '下发' },
          'edit',
          { code: 'cancel', text: '取消', confirm: true },
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 160,
    },
  ];
}
